<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>读了么！-正在售卖的图书</title>
    <link href="style/setUs.css" rel="stylesheet" type="text/css">
    <link href="layui/css/layui.css" rel="stylesheet" media="all">
    <script src="layui/layui.js"></script>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <style>
        .shelf {
            margin-top: 185px;
        }
    </style>
</head>
<body>
<header >
    <div class="head_d">
        <div class="leftH"></div>
        <div class="rightH"></div>
    </div>
</header>
<div class="gl">
    <span>读了么</span>卖家工作平台
</div>
<div class="manage">
    <div class="leftM">
        <div class="categoryFirst">
            <img src="image/c1.png">
            <span class="title">店铺管理</span>
            <ul class="list">
                <li><a href="Self_Information.jsp">我的店铺</a></li>
            </ul>
        </div>
        <hr/>
        <div class="category">
            <div>
                <img src="image/c2.png">
                <span class="title">交易管理</span></div>
            <ul class="list">
                <li>已卖出的商品</li>
                <li>评价管理</li>
            </ul>
        </div>
        <div class="category">
            <div class="list">
                <img src="image/c3.png">
                <span class="title">物流管理</span></div>
            <ul class="list">
                <li ><a href="shopSendGoods.jsp">发货</a></li>
                <li>物流工具</li>
            </ul>
        </div>
        <div class="category">
            <img src="image/c4.png">
            <span class="title">商品管理</span>
            <ul class="list">
                <li><a href="shopUpperShelf.html">发布书籍</a></li>
                <li><a href="shopSellBook.jsp">出售中的书籍</a></li>
            </ul>
        </div>
        <div class="categoryLast">
            <img src="image/c5.png">
            <span class="title">客户服务</span>
            <ul class="list">
                <li>退款管理</li>
                <li>违规记录</li>
            </ul>
        </div>
    </div>
    <div class="rightM">
        <div class="firstT"><span>我的店铺</span></div>
        <div class="lc-title"><span>正在售卖的图书：</span></div>
        <div class="shelf">
            <table id="shelf" lay-filter="test"></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="down">下架</button>
                </div>
            </script>
            <script type="text/html" id="barDemo1">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">下架</a>
            </script>
            <script>
                layui.use('table', function () {
                    var table = layui.table;
                    table.render({
                        elem: '#shelf',
                        height: 405,
                        page: true,
                        toolbar: '#toolbarDemo',
                        defaultToolbar: [],
                        url: 'http://localhost:8080/data/shopBooks',//获取数据的接口
                        cols: [[
                            {type: 'checkbox', fixed: 'left'},
                            {field: 'bookName', title: '书名', width: '150', sort: true, fixed: 'left'},
                            {field: 'author', title: '作者', width: '120'},
                            {field: 'press', title: '出版社', width: '120'},
                            {field: 'publishDate', title: '出版时间', width: '120'},
                            {field: 'price', title: '价格', width: '100', sort: true, edit: 'text'},
                            {field: 'remain', title: '库存', width: '100', sort: true, edit: 'text'},
                            {field: 'sell', title: '销量', width: '80', sort: true},
                            {fixed: 'right', width: 125, align: 'center', toolbar: '#barDemo1'}
                        ]]
                    });
                    //修改
                    table.on('edit(test)', function (obj) {
                        var data = obj.data;
                        var bookId = data.bookId;
                        var price = data.price;
                        var remain = data.remain;
                        // console.log(bookId);
                        // console.log(price);
                        // console.log(remain);
                        $.ajax({
                            url: 'http://localhost:8080/update/book',
                            type: 'GET',
                            dataType: 'json',
                            data: {'bookId': bookId, 'price': price, 'remain': remain,'method':'modify'},
                            success: function (data) {
                                if (data.result == '1') {
                                    alert("修改成功！");
                                }else alert("修改失败！");
                            }
                        })
                    });
                    //多件下架
                    table.on('toolbar(test)', function (obj) {
                        var checkStatus = table.checkStatus(obj.config.id);
                        var data = checkStatus.data;
                        // var checkStatu1=table.checkStatus('shelf');
                        // console.log(checkStatu1);
                        // console.log(data);
                        // console.log(checkStatus);
                        switch (obj.event) {
                            case 'down':
                                if (data.length == 0) {
                                    layer.msg('请选择至少一行！');
                                    return;
                                } else {
                                    var str = '';
                                    //将选择的几行数据的id集合在一起，用逗号隔开，直接删除这几个id
                                    for (var i = 0; i < data.length; i++) {
                                        str += data[i].bookId + ',';
                                    }

                                    // console.log(str);
                                    layer.confirm('是否下架这'+data.length+'本书籍？',{icon:3,title:'提示'},function (index) {
                                        $.ajax({
                                            url: 'http://localhost:8080/update/book?data='+str+'&method=deleteBatch',//多件下架的接口
                                            type: 'GET',
                                            dataType: 'json',
                                            data: {"data":str},
                                            success: function (data) {
                                                if (data.result== '1') {
                                                    alert("下架成功！");
                                                    table.reload('shelf',{'url':'http://localhost:8080/data/shopBooks'});

                                                }else alert("下架成功！")
                                            }
                                        });

                                        layer.close(index);

                                    })
                                }
                                break;
                        }
                    });
                    //单个下架
                    table.on('tool(test)', function (obj) {
                        var shopId = "${sessionScope.shop.shopId}";
                        var data = obj.data;
                        var layEvent = obj.event; //获得 lay-event 对应的值
                        var tr = obj.tr;
                        if (layEvent === 'del') {
                            layer.confirm('确认下架？', function (index) {
                                var bookId = data.bookId;
                                obj.del();
                                $.ajax({
                                    url: 'http://localhost:8080/update/book',//修改订单状态的接口
                                    type: 'GET',
                                    dataType: 'json',
                                    data: {'bookId': bookId,'shopId':shopId,'method':'delete'},
                                    success: function (data) {
                                        if (data.result=='1'){
                                            alert("下架成功！");
                                        } else{
                                            alert("下架失败！")
                                        }
                                    }
                                });
                                layer.close(index)
                            })
                        }
                    });
                });
            </script>
        </div>

    </div>
</div>
</body>
</html>
